﻿using System;
using System.Xml.Serialization;
using System.Web.Script.Serialization;

using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.CacheService;
using Fuse8.DomainFramework.BusinessService;

namespace Fuse8.ContentManagementFramework.Domain
{
    [Serializable]
    public partial class CmsMediaFile : CmsSiteEntityObject
    {
        #region Linked field

        private CmsMimeType _mimeType;

        #endregion

        #region Properties

        public virtual int? MimeTypeId { get; set; }
        
        public virtual string OriginalFileName { get; set; }

        public virtual string LinkedFileName { get; set; }

        public virtual long FileSize { get; set; }

        public virtual int? Height { get; set; }

        public virtual int? Width { get; set; }        

        #endregion

        #region Linked property

        [XmlIgnore]
        [ScriptIgnore]
        public CmsMimeType MimeType
        {
            get
            {
                if (_mimeType == null)
                {
                    if (MimeTypeId.HasValue && MimeTypeId != 0)
                    {
                        var businessFacade = new BusinessServiceFacade<CmsMimeType, int>();
                        if (businessFacade != null)
                        {
                            _mimeType = businessFacade.FindOne(MimeTypeId.Value);
                        }
                    }
                }

                return _mimeType;
            }
            set
            {
                MimeTypeId = value != null ? value.Id : new Nullable<int>();

                if (MimeTypeId.HasValue)
                {
                    if (_mimeType != null)
                    {
                        _mimeType = _mimeType.Id == MimeTypeId ? value : null;
                    }
                    else
                    {
                        _mimeType = value;
                    }
                }
                else
                {
                    _mimeType = null;
                }
            }
        }

        #endregion

        #region Constructor

        public CmsMediaFile()
        { }

        #endregion

        #region EntityObject override

        public override string TypeParameter
        {
            get
            {
                return "MediaFile";
            }
        }

        #endregion
    }
}
